/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.SwitchModel;
import com.thinkgem.jeesite.modules.sys.entity.SysMenuSwitch;
import com.thinkgem.jeesite.modules.sys.service.SysMenuSwitchService;
import com.thinkgem.jeesite.modules.sys.utils.SwitchUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 审批控制Controller
 * @author ZC
 * @version 2017-06-02
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysMenuSwitch")
public class SysMenuSwitchController extends BaseController {

	@Autowired
	private SysMenuSwitchService sysMenuSwitchService;
	
	@ModelAttribute
	public SysMenuSwitch get(@RequestParam(required=false) String id) {
		SysMenuSwitch entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysMenuSwitchService.get(id);
		}
		if (entity == null){
			entity = new SysMenuSwitch();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:sysMenuSwitch:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysMenuSwitch sysMenuSwitch, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysMenuSwitch> page = sysMenuSwitchService.findPage(new Page<SysMenuSwitch>(request, response), sysMenuSwitch); 
		model.addAttribute("page", page);
		return "modules/sys/sysMenuSwitchList";
	}

	@RequiresPermissions("sys:sysMenuSwitch:view")
	@RequestMapping(value = "form")
	public String form(SysMenuSwitch sysMenuSwitch, Model model) {
		model.addAttribute("sysMenuSwitch", sysMenuSwitch);
		return "modules/sys/sysMenuSwitchForm";
	}

	@RequiresPermissions("sys:sysMenuSwitch:edit")
	@RequestMapping(value = "save")
	public String save(SwitchModel switchModel, Model model, RedirectAttributes redirectAttributes) {

		for(SysMenuSwitch sysMenuSwitch:switchModel.getSysMenuSwitchs()){
			if (!beanValidator(model, sysMenuSwitch)){
				continue;
			}
			sysMenuSwitchService.save(sysMenuSwitch);
		}
        SwitchUtils.clearAll();
		addMessage(redirectAttributes, "修改审批控制成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysMenuSwitch/?repage";
	}
}